_Pragma("once");

#include <linux/serial.h>
#include <asm-generic/ioctls.h>
#include <sys/ioctl.h>

#include "SerialPortCommunicator485.h"



class DmxCommunicator : public SerialPortCommunicator {
public:
    using SerialPortCommunicator::SerialPortCommunicator;

    /** Default destructor */
    ~DmxCommunicator();


    int initialize(uint32_t baud) {
        switchRs485(true);

        setPort("dmx512 error:", 38400, 8, 'n', 2);

        serial_struct ss;
        if(ioctl(deviceFd, TIOCGSERIAL, &ss) < 0) {
            LOG_ERROR("BAUD: error to get the serial_struct info:%s\n", strerror(errno));
            return -1;
        }
//		printf("ss.line: %d, ss.custom_divisor: %d, ss.baud_base: %d, ss.irq: %d\n", ss.line, ss.custom_divisor, ss.baud_base, ss.irq);

        ss.flags = ASYNC_SPD_CUST;
        ss.custom_divisor = ss.baud_base/baud;
        if(ioctl(deviceFd, TIOCSSERIAL, &ss) < 0) {
            LOG_ERROR("BAUD: error to set serial_struct:%s\n", strerror(errno));
            return -2;
        }

//        serial_struct ss_set;
//        ioctl(deviceFd, TIOCGSERIAL, &ss_set);
//        printf("BAUD: success set baud to %d,custom_divisor=%d,baud_base=%d\n",baud, ss_set.custom_divisor, ss_set.baud_base);

        return 0;
    }


    ssize_t read(const std::string& exceptionTip, void * buffer, const size_t length) noexcept override;


};



